package com.discovery.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author xuebinlu
 * @Date 2018/5/17 13:44
 * @Version 1.0
 * @Description SessionInterceptor 拦截器
 **/
@Component
public class SessionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(request.getRequestURI());
        if (request.getRequestURI().equals("/userInfo/login")
                || request.getRequestURI().equals("/user/login")
                || request.getRequestURI().equals("/error")) {
            return true;
        }
        Object obj = request.getSession().getAttribute("userNanme");
        if (obj == null) {
            response.sendRedirect("/user/login");
            return false;
        }
        return true;
    }
}
